<?php

namespace App\Models;

use Carbon\Carbon;
use DB;
use App\Models\User;
use App\Models\TranslateImprovedRecord;
use App\Models\Vote;
use Illuminate\Database\Eloquent\Model;

class TranslateRecord extends Model
{
    //STATUS
    CONST STATUS_NOT_AUDIT = 1;
    CONST STATUS_AUDIT_PASS = 2;
    CONST STATUS_AUDIT_NOT_PASS = 3;

    //STATE
    CONST TYPE_FIRST_TRANSLATE = 1;
    CONST TYPE_AGAIN_TRANSLATE = 2;
    CONST TYPE_IMPROVE_TRANSLATE = 3;

    protected $fillable = [
        'translate_section_id',
        'status',
        'type',
        'vote_count',
        'translate_text',
        'translate_time',
        'translator_id',
        'auditor',
    ];

    public $timestamps = true;

    public function translateSection()
    {
        return $this->belongsTo(TranslateSection::class, 'translate_section_id', 'id');
    }

    public function translateImprovedRecords()
    {
        return $this->hasMany(TranslateImprovedRecord::class, 'translate_record_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'translator_id', 'id');
    }

    public function improvedUserIds()
    {
        $user_ids = explode('-', $this->improved_user_ids);

        return $user_ids;
    }

    public static function fetchTranslatorsInWeek()
    {

        $translators = self::select(DB::raw('COUNT(translator_id) AS record_count, translator_id'))
            ->where('updated_at', '>=', Carbon::now()->subWeek())
            ->where('status', self::STATUS_AUDIT_PASS)
            ->groupBy('translator_id')
            ->orderBy('record_count', 'DESC')
            ->get();


        foreach ($translators AS $key => $translator) {
            $user = User::where('id', $translator['translator_id'])
                ->get();

            $translators[$key]['avatar'] = $user[0]->present()->gravatar(100);
            $translators[$key]['name'] = $user[0]->name;
            $translators[$key]['id'] = $user[0]->id;
        }

        return $translators;

    }

    public static function fetchTranslatorsInMonth()
    {

        $translators = self::select(DB::raw('COUNT(translator_id) AS record_count, translator_id'))
            ->where('updated_at', '>=', Carbon::now()->subMonth())
            ->where('status', self::STATUS_AUDIT_PASS)
            ->groupBy('translator_id')
            ->orderBy('record_count', 'DESC')
            ->get();


        foreach ($translators AS $key => $translator)
        {
            $user = User::where('id', $translator['translator_id'])
                ->get();

            $translators[$key]['avatar'] = $user[0]->present()->gravatar(100);
            $translators[$key]['name'] = $user[0]->name;
            $translators[$key]['id'] = $user[0]->id;
        }

        return $translators;

    }

    public static function fetchTranslatorRank($limit = 3)
    {
        $translators = self::select(DB::raw('COUNT(translator_id) AS record_count, translator_id'))
            ->where('status', self::STATUS_AUDIT_PASS)
            ->groupBy('translator_id')
            ->orderBy('record_count', 'DESC')
            ->limit($limit)
            ->get();


        foreach ($translators AS $key => $translator) {
            $user = User::where('id', $translator['translator_id'])
                ->get();
            $translators[$key]['avatar'] = $user[0]->present()->gravatar(100);
            $translators[$key]['name'] = $user[0]->name;
            $translators[$key]['id'] = $user[0]->id;
            if ($key == 0) {
                $translators[$key]['rank_name'] = '冠军';
            } elseif ($key == 1) {
                $translators[$key]['rank_name'] = '亚军';
            } elseif ($key == 2) {
                $translators[$key]['rank_name'] = '季军';
            }
        }

        return $translators;
    }

    public function setAttributes($attrs)
    {
        self::update($attrs);
    }

    public function getAttrbute($attr_name)
    {
        return $this->attributes[$attr_name];
    }

    public function votes()
    {
        return $this->morphMany(Vote::class, 'votable');
    }

    public function getHumansTime()
    {
        return Carbon::createFromTimeString($this->created_at)->diffForHumans();
    }

}

